<template>
	<view style="background-color: #F1F4F9;">
		<view class="addressList" v-if="addressList.length !== 0">
			<view v-for="(item,index) in addressList" :key="index">
				<view class="areaList">
					<view class="flx" @click="switchAddress(item.userName,item.userMobile,item.recycleAddress)">
						<view class="name">{{item.userName | nameFilter}}</view>
						<view class="mobile">{{item.userMobile | phoneStart }}</view>
					</view>
					<view class="address" @click="switchAddress(item.userName,item.userMobile,item.recycleAddress)">
						{{item.recycleAddress}}
					</view>
					<view class="line"></view>
					<view class="flx">
						<!-- <radio-group @change="radioChange(item.id)"> -->
						<view class="" v-if="item.isDefault">
							<radio color="var(--global-bg-color)" class="radio-class" :checked="item.isDefault" style=""/> 默认地址
						</view>
						<view class="" v-if="item.isDefault == false">
							
						</view>
						<!-- </radio-group> -->
						<view class="flx-right">
							<view class="edit"
								@click="edit(item.id,item.userName,item.userMobile,item.recycleAddress,item.isDefault)">
								<image src="../../static/images/edit.png" mode=""></image>
								<view class="">编辑</view>
							</view>
							<view class="edit del" @click="del(item.id)">
								<image src="../../static/images/del.png" mode=""></image>
								<view class="">删除</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="status" :iconSize="17"  />
		</view>
		<view class="" v-else>
			<view class="center">
				<img src="../../static/images/no_data.png" class="no_data">
				<div>暂无内容~</div>
			</view>
		</view>
		<navigator url="/pagesA/addAddress/addAddress" open-type="navigate">
			<view class="addAddress">添加新地址</view>
		</navigator>
	</view>
</template>

<script>
	import request from '../../utils/request.js'
	export default {
		data() {
			return {
				isFinished: false, // 是否已加载完所有数据
				page: 1,
				limit: 5,
				addressList: [
					// {
					// userName: '张三',
					// userMobile: 13673984186,
					// recycleAddress: '浙江省  杭州市  临平区  南苑街道时代广场1号楼时代广场1号楼 时代广场1号楼',
					// isDefault: false
					// }
				],
				//需要加载的数据必须要触碰到每一页的底部才会加载数据
				status: 'loadmore',
				suo: true,
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				finished: 'nomore', // 加载状态
				//需要加载的数据必须要触碰到每一页的底部才会加载数据
				status: 'loadmore',
			};
		},
		methods: {
			// 切换地址
			switchAddress(userName, userMobile, recycleAddress) {
				var addressInfo = {
					userName: userName,
					userMobile: userMobile,
					recycleAddress: recycleAddress
				}
				uni.navigateTo({
					url: '/pagesA/call-center/call-center'
				});
				uni.setStorageSync('addressInfo', addressInfo);
			},
			getRecycleAddressData() {
				let that = this
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				request.get('/api/recycle/address/page', {
					page: this.page,
					limit: this.limit
				}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						that.addressList = res.data.list
						if (res.data.list.length == 0) {
							that.status = 'nomore';
							that.suo = false
						} else if (res.data.list.length < 5) {
							that.status = 'nomore';
							that.suo = false
						} else if (res.data.list.length ==5) {
							that.status = 'loadmore';
							that.suo = true
						}
					} else if (res.code == 5) {
						
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			onReachRecycleAddressData() {
				let that = this
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				request.get('/api/recycle/address/page', {
					page: that.page,
					limit: that.limit
				}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {

						console.log(res.data, '2222211111111111111')
						that.addressList = that.addressList.concat(res.data.list)
						if (res.data.list.length == 0) {
							that.status = 'nomore';
							that.suo = false
						} else if (res.data.list.length < 5) {
							that.status = 'nomore';
							that.suo = false
						} else if (res.data.list.length == 5) {
							that.status = 'loadmore';
							that.suo = true
						}
					} else if (res.code == 5) {
						
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			edit(id, userName, userMoblie, recycleAddress, isDefault) {
				console.log(id, userName, userMoblie, recycleAddress, isDefault, 'addressDetail')
				//  注：如果需要传递多个参数使用&符号连接即可，示例：
				uni.navigateTo({
					url: `/pagesA/editAddress/editAddress?id=${id}&userName=${userName}&userMoblie=${userMoblie}&recycleAddress=${recycleAddress}&isDefault=${isDefault}`
				});
			},
			del(id) {
				let that = this

				console.log(id, '111111111111')
				uni.showModal({
					content: '你确定要删除该收货地址吗？',
					success: function(res) {

						if (res.confirm) {
							request.del(`/api/recycle/address/${id}`,

							).then(res => {
								if (res.code == 0) {
									that.getRecycleAddressData()
									uni.showToast({
										title: '删除成功',
										icon: 'none',
										duration: 2000
									})

								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 2000
									})
								}
							})
						} else if (res.cancel) {

						}
					}
				});
			},

		},
		onShow() {
			var that = this
			this.getRecycleAddressData()
		},
		onLoad() {
			// 设置导航栏的颜色和标题颜色
			// uni.setNavigationBarColor({
			// 	frontColor: "#000000", //文字颜色
			// 	backgroundColor: "#FFFFFF" //底部背景色
			// })
			
		},
		filters: {
			nameFilter(value) {
				if (!value) return '';
				return value[0] + '*'.repeat(value.length - 1);
			}
		},
		onReachBottom() {
			// this.page = 1;
			if (this.suo) { //定义个小锁，当没有数据时锁住suo
				// this.status = 'loading';
				// this.page = ++this.page;
				// this.page = 1;
				
					this.status = 'loading';
					this.page = ++this.page;
		
					setTimeout(() => {
						this.onReachRecycleAddressData()
					}, 1000)
					return;
				
				
			}
		},
		
	}
</script>

<style lang="less" scoped>
	page{
		background-color: #F1F4F9;
	}
	.addressList {
		padding-bottom: 120rpx;
	}

	.areaList {
		background: #FFFFFF;
		margin-bottom: 16rpx;
		padding: 30rpx 24rpx 6rpx;

		.flx {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-family: Source Han Sans SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #000000;
			line-height: 34rpx;
			margin-bottom: 22rpx;
		}

		.address {
			font-family: Source Han Sans SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #666;
			line-height: 36rpx;
		}

		.line {
			width: 100%;
			height: 1rpx;
			background: #EBEBEB;
			margin: 20rpx auto;
		}

		uni-radio .uni-radio-input {
			border-radius: 0 !important;
		}

		.flx-right {
			display: flex;
			align-items: center;

			.edit {
				display: flex;
				align-items: center;

				image {
					width: 32rpx;
					height: 31rpx;
					margin-right: 18rpx;
				}
			}

			.del {
				margin-left: 90rpx;

				image {
					width: 29rpx;
					height: 32rpx;
				}
			}
		}
	}

	.addAddress {
		position: fixed;
		bottom: 5vh;
		left: 0;
		width: 90%;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		font-family: Source Han Sans SC;
		font-weight: 500;
		font-size: 36rpx;
		color: #FFFFFF;
		opacity: 0.85;
		background-color: var(--global-bg-color);
		border-radius: 50rpx;
		margin: 0 5%;
	}

	.center {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		text-align: center;
		margin-top: 370rpx;
	}

	.no_data {
		width: 380rpx;
		height: 300rpx;
	}

	.no_data {
		width: 380rpx;
		height: 300rpx;
	}
</style>